package cn.stylefeng.guns.core.util;

import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.sys.core.util.FtpUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class QRCodeUtil {

    public static String createQr(String link,boolean isRemoveSide) {
        if (link == null || link.length() <= 0) {
            System.err.println("二维码内容不能为空");
            return "";
        }

        try {
            Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
            // 指定编码格式
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            // 指定纠错级别(L--7%,M--15%,Q--25%,H--30%)
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            BitMatrix bitMatrix = new MultiFormatWriter().encode(link, BarcodeFormat.QR_CODE, 350, 350, hints);
            //去除白边
            if (isRemoveSide) {
                bitMatrix = deleteWhite(bitMatrix);
            }

            String path = "D://qr/" + System.currentTimeMillis() + ".png";

            //设置请求头
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            MatrixToImageWriter.writeToStream(bitMatrix, "png", fileOutputStream);
            fileOutputStream.close();

            return path;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }



    //去除二维码白边
    private static BitMatrix deleteWhite(BitMatrix matrix) {
        int[] rec = matrix.getEnclosingRectangle();
        int resWidth = rec[2] + 1;
        int resHeight = rec[3] + 1;

        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
        resMatrix.clear();
        for (int i = 0; i < resWidth; i++) {
            for (int j = 0; j < resHeight; j++) {
                if (matrix.get(i + rec[0], j + rec[1])) resMatrix.set(i, j);
            }
        }
        return resMatrix;
    }


//    public static void main(String[] args) {
//    	getQrcode("13800138000",300,300,"1.png");
//    }


    public static String getQrcode(String text, int width, int height, String filekey) {

        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;

        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
            MatrixToImageWriter.writeToStream(bitMatrix, "png", os);
            byte[] bytes = os.toByteArray();
//            AliyunOss oss = new AliyunOss();
//            String url = oss.ossUploadByteStream(filekey, bytes);

            //生成文件的唯一id
            String fileId = IdWorker.getIdStr();
            String fileName=fileId+".png";

            //这里写文件的输入流
            InputStream inputStream = new ByteArrayInputStream(bytes);

            //连接ftp服务
            FTPClient ftpClient = FtpUtil.connection("101.35.53.127", 21, "bsh","123456");

            Boolean status = FtpUtil.uploadFile(ftpClient, "/bsh", "/app", fileName, inputStream);

            String url = ConstantsContext.getFileUrl() + "/bsh" + "/app" + "/" + fileName;

            System.out.println(url);
            return url;
        } catch (WriterException | IOException e) {
            e.printStackTrace();
            return null;
        }finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }


    }

}
